package com.situ.hospital.servlet;

import com.situ.hospital.pojo.Department;
import com.situ.hospital.pojo.query.DepartmentQuery;
import com.situ.hospital.service.IDepartmentService;
import com.situ.hospital.service.impl.DepartmentServiceImpl;
import com.situ.hospital.util.JSONUtil;
import com.situ.hospital.util.MyBeanUtil;
import com.situ.hospital.util.PageResult;
import com.situ.hospital.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {

    private IDepartmentService departmentService = new DepartmentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectAll":
                selectAll(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.update");
        Department department = MyBeanUtil.copyToBean(req,Department.class);
        departmentService.update(department);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.selectById");
        String id = req.getParameter("id");
        Department department = departmentService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(department));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.add");
        Department department = MyBeanUtil.copyToBean(req, Department.class);
        departmentService.add(department);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        departmentService.deleteByAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.deleteById");
        String id = req.getParameter("id");
        departmentService.deleteById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.selectByPage");
        DepartmentQuery departmentQuery = MyBeanUtil.copyToBean(req, DepartmentQuery.class);
        PageResult<Department> pageResult = departmentService.selectByPage(departmentQuery);
        JSONUtil.toJSON(resp, pageResult);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("DepartmentServlet.selectAll");
        List<Department> list = departmentService.selectAll();
        JSONUtil.toJSON(resp, list);
    }
}
